<template>
  <cu-custom bg-color="bg-white text-black solid-bottom" is-back bg-image="">
    <template #backText>返回</template>
  </cu-custom>
  <view class="container">
    <view class="cu-bar bg-white solid-bottom">
      <view class="action text-black text-bold">
        <text class="cuIcon-titles text-orange"></text>
        <text class="text-xl">教育背景</text>
      </view>
      <view class="action">
        <button
          class="cu-btn bg-gradual-blue shadow"
          form-type="submit"
          @click="save"
        >
          保存
        </button>
      </view>
    </view>
  </view>
  <form>
    <view class="cu-form-group solid-bottom">
      <view class="title">学校名称</view>
      <input v-model="form.schoolName" placeholder="请输入技能名称" />
    </view>
    <view class="cu-form-group solid-bottom">
      <view class="title">主修专业</view>
      <input v-model="form.majorName" placeholder="请输入技能名称" />
    </view>
    <view class="cu-form-group solid-bottom">
      <view class="title">在职时间</view>
      <uni-datetime-picker v-model="form.time" :border="true" type="daterange">
      </uni-datetime-picker>
    </view>
    <view class="cu-form-group solid-bottom">
      <view class="title">学历</view>
      <uni-data-select
        v-model="form.school"
        :localdata="range"
      ></uni-data-select>
    </view>
  </form>
</template>

<script setup lang="ts">
import { reactive, toRefs } from "vue";
import { onLoad } from "@dcloudio/uni-app";
const { range } = reactive({
  range: [
    { value: 0, text: "小学" },
    { value: 1, text: "初中" },
    { value: 2, text: "高中" },
    { value: 3, text: "大专" },
    { value: 4, text: "本科" },
    { value: 5, text: "研究生" },
    { value: 6, text: "博士" },
  ],
});
const state = reactive({
  form: {
    time: ["2019-6", "2023-9"],
    school: 0,
    schoolName: "二仙桥职业学院",
    majorName: "计算机应用",
  },
});
const { form } = toRefs(state);
let index: any = null;
let educationalBg: any = [];
onLoad((option: any) => {
  if (option.index) {
    index = option.index;
  }
  uni.getStorage({
    key: "educationalBg",
    success: ({ data }) => {
      if (option.index) {
        form.value = data.data[index];
      }
      if (data.data) {
        educationalBg = data.data;
      }
    },
  });
});

const save = () => {
  let data = null;
  if (index != undefined) {
    educationalBg[index] = form.value;
    data = [...educationalBg];
  } else {
    data = [...educationalBg, form.value];
  }

  uni.setStorage({
    key: "educationalBg",
    data: {
      data: data,
      type: 1,
    },
  });
  uni.switchTab({ url: "/pages/index/index" });
};
</script>

<style></style>
